<template>
  <el-dialog
    :visible.sync="param.visible"
    title="添加行业"
    center
    :close-on-click-modal="false"
  >
    <Form1
      ref="addForm"
      :rules="rules"
      :form="addForm"
      :items="items"
      :save="addSave"
    >
    </Form1>
  </el-dialog>
</template>
<script>
import Form1 from "@/components/Form1.vue";
export default {
  name: "Add",
  components: { Form1 },
  props: {
    param: {
      type: Object,
      default: () => {},
    },
    typeId: { type: Number, default: () => {} },
  },
  data() {
    return {
      addForm: {
        //插入表单
        name: "",
        parent_id: null,
      },
      //验证规则
      rules: {
        name: [{ required: true, message: "行业名称不能为空" }],
      },

      items: [
        {
          type: "text",
          label: "行业名称：",
          prop: "name",
          name: "name",
          placeholder: "请输入行业名称",
        },
        {
          type: "select",
          label: "上级类别",
          prop: "parent_id",
          name: "parent_id",
          placeholder: "请输入上级类别",
          options: [],
        },
      ],
    };
  },

  created() {
    //初始化options
    this.getRequest("/profession/query").then((res) => {
      if (res) {
        console.log(res.data);
        let arr = res.filter((item) => item.parent_id == 0);
        arr.forEach((item) => {
          let param = { value: item.id, label: item.name };
          this.items[1].options.push(param);
        });
      }
    });
  },
  methods: {
    //   保存
    addSave() {
      let flag = this.$refs["addForm"].validateForm();
      if (flag) {
        //   保存
        this.postRequest("/profession/", this.addForm).then((res) => {
          if (res) {
            this.$message.success(res.message);
            this.param.close(); //关闭
            this.param.callback(); //重新查询
          }
        });
      }
    },
  },
};
</script>
<style></style>
